unsigned char bfColorMix(signed int iXMix, signed int iYMix, unsigned char bFlux)
{
    unsigned char i;                    //Variable used for indexing
    signed long lScratch1;              //Variable used to hold many temporary values
    signed int iScratch2;               //Variable used to hold many temporary values
    unsigned int* pwShort;              //Variable used as the pointer to an element of the sLED_Data structure
    unsigned long dScaleFactor;         //Variable used to hold a scaling factor
    
    pwShort = &sLED_Data.wRedLEDX;      //Set pointer to point at first LED's x coordinate variable
    
    for(i=0; i<NUM_LEDS; i++)                  //Index through this for loop once for each LED color.
    {
        lScratch1 = (signed int)*(pwShort+2*i) - iXMix;        //Do math operation: x(i) - xMix            
        mul_16x16_32(lScratch1, 1000, &lScratch1);             //Do math operation: (x(i) - xMix) * 1000
        
        iScratch2 = (signed int)*(pwShort+2*i+1) >> 1;         //Do math operation: y(i) / 2, this will be for rounding accuracy
        
        if(lScratch1 < 0){                                     
            lScratch1 -= (signed long)iScratch2;}    //Do math operation: ((x(i) - xMix) * 1000) +- (y(i) / 2)  
        else{                                        //Will subract if (x(i) - xMix) * 1000 is negative, otherwise it will add
            lScratch1 += (signed long)iScratch2;}    //This is for rounding correctly
            
        
        lScratch1 /= (signed long)*(pwShort+2*i+1);  //Do math operation: (((x(i) - xMix) * 1000) +- (y(i) / 2)) / y(i)
        aiOpMatrix[0][i] = (signed int)lScratch1;    //Put that value into the ith column of the first row of the operation matrix
    }
    
    pwShort = &sLED_Data.wRedLEDY;        //Set pointer to point at first LED's y coordinate variable
    
    for(i=0; i<NUM_LEDS; i++)             //Index through this for loop once for each LED color.
    {
        lScratch1 = (signed int)*(pwShort + 2*i) - iYMix;    //Do math operation: y(i) - yMix               
        mul_16x16_32(lScratch1, 1000, &lScratch1);           //Do math operation: (y(i) - yMix) * 1000
        
        iScratch2 = (signed int)*(pwShort+2*i) >> 1;         //Do math operation: y(i) / 2, this will be for rounding accuracy
        
        if(lScratch1 < 0){
            lScratch1 -= (signed long)iScratch2;}    //Do math operation: ((y(i) - yMix) * 1000) +- (y(i) / 2)
        else{                                        //Will subract if (y(i) - yMix) * 1000 is negative, otherwise it will add
            lScratch1 += (signed long)iScratch2;}    //This is for rounding correctly
        
        lScratch1 /= (signed int)*(pwShort+2*i);     //Do math operation: (((y(i) - yMix) * 1000) +- (y(i) / 2)) / y(i)                 
        aiOpMatrix[1][i] = (signed short)lScratch1;  //Put that value into the ith column of the second row of the operation matrix
    }
    
    for(i=0; i<NUM_LEDS; i++)        //Index through this for loop once for each LED color.
    {
        
        aiOpMatrix[2][i] = 1;        //Put value of 1 into ith column of the third row of the operation matrix
    }
    
    
    MatrixInverse_3x3(aiOpMatrix, alOpVector);    //Do an Inversion operation on the matrix
                                                  //This matrix inversion will return the values in the third column of the matrix
                                                  //as a vector of values into the alOpVector arrray. It returns only these three
                                                  //values because they are the only ones of importance as far as calculating dimming
                                                  //values is concerned.

    for(i=0; i<NUM_LEDS; i++)        //Index through this for loop once for each LED color.
    {
        alOpVector[i] *= (signed long)wDegradedTotalLumens;    //Multiply each element of the vector by the adjusted total lumen value
                                                               //that is requested
        
        if(alOpVector[i] < 0)                        //This if statement checks to see if any of the values in the vector are negative
        {                                            //If they are negative, that signifies that the LEDs can't create the color requested
            sLED_Data.abDimmingValues[RED] = 1;      
            sLED_Data.abDimmingValues[GREEN] = 1;    //Therefore, if they are negative set each dimming value to 1, which is the same as OFF
            sLED_Data.abDimmingValues[BLUE] = 1;
            
            return INVALID_COLOR;                    //Return error code for invalid color request error
        }
        
        alOpVector[i] <<= 8;                //Do math operation: alOpVector(i) / 256, this is to scale the values correctly
        
        iScratch2 = awLumens[i] >> 1;       //Do math operation: Lumens(i) / 2, this will be to round correctly
        
        if(alOpVector[i] < 0){                            //Do math operation: (alOpVector(i) +- (Lumens(i) / 2)
            alOpVector[i] -= (signed long)iScratch2;}     //Will subract if alOpVector(i) is negative, otherwise it will add   
        else{                                             //This is for rounding correctly         
            alOpVector[i] += (signed long)iScratch2;}
        
        
        alOpVector[i] /= (signed long)awLumens[i];        //Do math operation: (alOpVector(i) +- (Lumens(i) / 2)) / Lumens(i)  
        alOpVector[i] *= 2550;                            //Do math operation: ((alOpVector(i) +- (Lumens(i) / 2)) / Lumens(i)) * 2550
                                                          //The above math operation is to scale the values correctly
        
        if(alOpVector[i] < 0){            //This if statement will ensure that rounding occurs correctly. It is in preparation
            alOpVector[i] -= 5000;}       //for the divide operation below the if statement.
        else{
            alOpVector[i] += 5000;}
        
        alOpVector[i] /= 10000;           //Do math operation: alOpVector(i) / 10000, this is the final scaling operation
                                          //At this point, the alOpVector values are the 8bit dimming values for the LEDs
    }
    
       
    if(alOpVector[RED] > alOpVector[GREEN]){    //The following if chain simply puts the largest of the three dimming
        lScratch1 = alOpVector[RED];            //values in the lScratch1 variable.
    }else{
        lScratch1 = alOpVector[GREEN];
    }
    if(lScratch1 < alOpVector[BLUE]){
        lScratch1 = alOpVector[BLUE];   
    }
    
     
    if(lScratch1 > DIMMER_MAX)       //Scaling will be required if any dimming values are over the maximum possible
    {
        dScaleFactor = (unsigned long)alOpVector[RED];        //Start with the red dimming value
        if(dScaleFactor <= 1){
            dScaleFactor = 1;}    //If original dimming value was 1 or less, set it to 1.  This prevents rollover.
        else{                     //Otherwise, proceed with normal scaling.
            dScaleFactor = (dScaleFactor << 8) - dScaleFactor;    //Do math: (RedDimValue * 255)
            dScaleFactor /= (unsigned long)lScratch1;}            //Do math: (RedDimValue * 255) / LargestDimmingValue
        
        sLED_Data.abDimmingValues[RED] = (unsigned char)dScaleFactor;    //Set dimming value in sLED_Data structure to the scaled dimming value
        
        
        dScaleFactor = (unsigned long)alOpVector[GREEN];      //Proceed with the green dimming value
        if(dScaleFactor <= 1){
            dScaleFactor = 1;}    //If original dimming value was 1 or less, set it to 1.  This prevents rollover.
        else{                     //Otherwise, proceed with normal scaling.
            dScaleFactor = (dScaleFactor << 8) - dScaleFactor;    //Do math: (RedDimValue * 255)
            dScaleFactor /= (unsigned long)lScratch1;}            //Do math: (RedDimValue * 255) / LargestDimmingValue
        
        sLED_Data.abDimmingValues[GREEN] = (unsigned char)dScaleFactor;  //Set dimming value in sLED_Data structure to the scaled dimming value
        
        dScaleFactor = (unsigned long)alOpVector[BLUE];        //Proceed with the blue dimming value
        if(dScaleFactor <= 1){
            dScaleFactor = 1;}    //If original dimming value was 1 or less, set it to 1.  This prevents rollover.
        else{                     //Otherwise, proceed with normal scaling.
            dScaleFactor = (dScaleFactor << 8) - dScaleFactor;    //Do math: (RedDimValue * 255)
            dScaleFactor /= (unsigned long)lScratch1;}            //Do math: (RedDimValue * 255) / LargestDimmingValue
        
        sLED_Data.abDimmingValues[BLUE] = (unsigned char)dScaleFactor;  //Set dimming value in sLED_Data structure to the scaled dimming value
        
        return LUMEN_ERROR;       //Return the error code signifying too many lumens were requested
    }
    
                                     
                                     //This point is reached if there were no previous color mixing errors.
                                     //In other words, the requested color is valid, and too many lumens were not requested.
    
    for(i=0; i<NUM_LEDS; i++)        //Index through this for loop once for each LED color.
    {
        sLED_Data.abDimmingValues[i] = (unsigned char)alOpVector[i];    //Set each dimming value in the sLED_Data structure
        if(sLED_Data.abDimmingValues[i] == 0)                           //If any of them happen to be zero, set them to 1 instead
        {                                                               //The dimming value 1 is the same thing as OFF
            sLED_Data.abDimmingValues[i] = 1;
        } 
    }
 
    return NO_ERROR;        //Return the code that signifies there were no errors.
}

//--------------------------



unsigned char LED4_fColorMix(COORD sTarget, LED* pLED, unsigned char* pDimArray)
{
    unsigned char i;                    //Variable used for indexing
    signed long lScratch1;              //Variable used to hold many temporary values
    signed int iScratch2;               //Variable used to hold many temporary values
    unsigned long dScaleFactor;         //Variable used to hold a scaling factor
    signed int aiOpMatrix[3][3];
    signed long alOpVector[3];
    
    
    for(i=0; i<NUM_LEDS; i++)                  //Index through this for loop once for each LED color.
    {
        lScratch1 = (signed long)((*(pLED + i)).wLED_x - sTarget.wMixed_x);        //Do math operation: x(i) - xMix            
        lScratch1 *= 1000;             //Do math operation: (x(i) - xMix) * 1000
        
        iScratch2 = (signed int)(*(pLED + i)).wLED_y >> 1;        //Do math operation: y(i) / 2, this will be for rounding accuracy
        
        if(lScratch1 < 0){                                     
            lScratch1 -= (signed long)iScratch2;}    //Do math operation: ((x(i) - xMix) * 1000) +- (y(i) / 2)  
        else{                                        //Will subract if (x(i) - xMix) * 1000 is negative, otherwise it will add
            lScratch1 += (signed long)iScratch2;}    //This is for rounding correctly
            
        
        lScratch1 /= (signed long)(*(pLED + i)).wLED_y;  //Do math operation: (((x(i) - xMix) * 1000) +- (y(i) / 2)) / y(i)
        aiOpMatrix[0][i] = (signed int)lScratch1;    //Put that value into the ith column of the first row of the operation matrix
    }
    
    for(i=0; i<NUM_LEDS; i++)             //Index through this for loop once for each LED color.
    {
        lScratch1 = (signed int)(*(pLED + i)).wLED_y - sTarget.wMixed_y;    //Do math operation: y(i) - yMix               
        lScratch1 *= 1000;                      //Do math operation: (y(i) - yMix) * 1000
        
        iScratch2 = (signed int)(*(pLED + i)).wLED_y >> 1;         //Do math operation: y(i) / 2, this will be for rounding accuracy
        
        if(lScratch1 < 0){
            lScratch1 -= (signed long)iScratch2;}    //Do math operation: ((y(i) - yMix) * 1000) +- (y(i) / 2)
        else{                                        //Will subract if (y(i) - yMix) * 1000 is negative, otherwise it will add
            lScratch1 += (signed long)iScratch2;}    //This is for rounding correctly
        
        lScratch1 /= (signed int)(*(pLED + i)).wLED_y;     //Do math operation: (((y(i) - yMix) * 1000) +- (y(i) / 2)) / y(i)                 
        aiOpMatrix[1][i] = (signed short)lScratch1;  //Put that value into the ith column of the second row of the operation matrix
    }
    
    for(i=0; i<NUM_LEDS; i++)        //Index through this for loop once for each LED color.
    {
        
        aiOpMatrix[2][i] = 1;        //Put value of 1 into ith column of the third row of the operation matrix
    }
    
    
    i = LED4_fMatrixInverse_3x3(aiOpMatrix, alOpVector);    //Do an Inversion operation on the matrix
                                                  //This matrix inversion will return the values in the third column of the matrix
                                                  //as a vector of values into the alOpVector arrray. It returns only these three
                                                  //values because they are the only ones of importance as far as calculating dimming
                                                  //values is concerned.
    if(i == INVALID_COLOR)
    {
        return INVALID_COLOR;
    }
    
    
    
    
    for(i=0; i<NUM_LEDS; i++)        //Index through this for loop once for each LED color.
    {
    
        alOpVector[i] *= (signed long)wDegradedTotalLumens;    //Multiply each element of the vector by the adjusted total lumen value
                                                               //that is requested
        alOpVector[i] *= 100;    //Multiply each element of a constant of 100 to scale things correctly
        
        alOpVector[i] <<= 8;                //Do math operation: alOpVector(i) / 256, this is to scale the values correctly
        
        iScratch2 = (signed int)(*(pLED + i)).wRatedFlux >> 1;       //Do math operation: Lumens(i) / 2, this will be to round correctly
        
        if(alOpVector[i] < 0){                            //Do math operation: (alOpVector(i) +- (Lumens(i) / 2)
            alOpVector[i] -= (signed long)iScratch2;}     //Will subract if alOpVector(i) is negative, otherwise it will add   
        else{                                             //This is for rounding correctly         
            alOpVector[i] += (signed long)iScratch2;}
        
        
        alOpVector[i] /= (signed long)(*(pLED + i)).wRatedFlux;        //Do math operation: (alOpVector(i) +- (Lumens(i) / 2)) / Lumens(i)  
    }
    
       
    if(alOpVector[LED1] > alOpVector[LED2]){    //The following if chain simply puts the largest of the three dimming
        lScratch1 = alOpVector[LED1];            //ratios in the lScratch1 variable.
    }else{
        lScratch1 = alOpVector[LED2];
    }
    if(lScratch1 < alOpVector[LED3]){
        lScratch1 = alOpVector[LED3];   
    }
    
    
    for(i=0; i<NUM_LEDS; i++)
    {
    
        alOpVector[i] *= 255;
        alOpVector[i] += (lScratch1 >> 1);
        alOpVector[i] /= lScratch1;
        
        *(pDimArray + i) = (unsigned char)alOpVector[i];    //Set each dimming value in the sLED_Data structure
        if(*(pDimArray + i) == 0)                           //If any of them happen to be zero, set them to 1 instead
        {                                                               //The dimming value 1 is the same thing as OFF
            *(pDimArray + i) = 1;
        } 
    
    }

                                     
                                     //This point is reached if there were no previous color mixing errors.
                                     //In other words, the requested color is valid, and too many lumens were not requested.
 
    return NO_ERROR;        //Return the code that signifies there were no errors.
}



